package com.example.tms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.tms.bean.JiGuang;
import com.example.tms.httpclient.JiguangPush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MesController {

    private JiguangPush jiguangPush;

    @Autowired
    public MesController(JiguangPush jiguangPush){
        this.jiguangPush = jiguangPush;
    }

    //新建模版
    @RequestMapping(value = "msg/templates",method = RequestMethod.POST)
    public JSONObject postTemplates(){
        return jiguangPush.jiguangPost("https://api.sms.jpush.cn/v1/templates", JSON.toJSONString(templatesJson()));
    }

    //查询模版
    @RequestMapping(value = "templates/{temp_id}",method = RequestMethod.GET)
    public JSONObject getTemplates(@PathVariable String temp_id){
        return jiguangPush.jiguangGet("https://api.sms.jpush.cn/v1/templates/"+temp_id);
    }

    //发送验证码
    @RequestMapping(value = "codes", method = RequestMethod.POST)
    public JSONObject postCodes(@RequestBody JiGuang jiGuang){
        return jiguangPush.jiguangPost("https://api.sms.jpush.cn/v1/codes", JSON.toJSONString(jiGuang));
    }

    //验证验证码
    @RequestMapping(value = "codes/valid", method = RequestMethod.POST)
    public JSONObject validCodes(@RequestBody JiGuang jiGuang){
        return jiguangPush.jiguangPost("https://api.sms.jpush.cn/v1/codes/"+jiGuang.getMsg_id()+"/valid",JSON.toJSONString(jiGuang));
    }

    /**
     * 短信模版
     */
    private static JSONObject templatesJson(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("template","您的手机验证码：{{code}}，60s后过期，请勿泄露。如非本人操作，请忽略此短信。谢谢！");
        jsonObject.put("type",1);
        jsonObject.put("ttl",60);
        jsonObject.put("remark","短信验证");
        return jsonObject;
    }

}
